package stud.mpgame.flash.game;

import flash.utils.TypedDictionary;

enum GameEvent {
     playerAdded;
     playerRemoved;
}
 
class GameEventDispatcher {
    
    private var listeners:TypedDictionary < GameEvent, Array< GameEvent -> Dynamic -> Void > >;
     
    public function new() { 
        this.listeners = new TypedDictionary< GameEvent, Array< GameEvent -> Dynamic -> Void > >();
    }
    
    public function addGameEventListener(event:GameEvent, _function:GameEvent -> Dynamic -> Void):Void {
        var listener = listeners.get(event);
        if (listener == null){
            listener = new Array < GameEvent -> Dynamic -> Void >();
            listeners.set(event, listener);
        }
        listener.push(_function);  
    }
    
    public function removeGameEventListener(event:GameEvent, _function:GameEvent -> Dynamic -> Void):Void {
        if (listeners.exists(event)) {
            listeners.get(event).remove(_function);
        }
    }
    
    public function notifyListeners(event:GameEvent, data:Dynamic) {
        var listeners:Array < GameEvent -> Dynamic -> Void > = listeners.get(event);
		if(listeners != null)
			for (listener in listeners)
				listener(event, data);
    }
    
}